

  <title>任务包管理</title>

  <div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
      <a lay-href="">生产任务</a>
      <a><cite>任务包列表</cite></a>
    </div>
  </div>

  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
      <div class="layui-col-md12">
        <div class="layui-card">
          <div class="layui-card-body">
              <script type="text/html" id="toolbarDemo">
                  <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="addNewPackage">新增任务包</button>
                    <button class="layui-btn layui-btn-warm  layui-btn-sm" lay-event="estimate">任务评估</button>
                  </div>
              </script>
              <table class="layui-hide" id="table01" lay-filter="table01"></table>
              <script type="text/html" id="test-table-operate-barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="ed">编辑任务</a>
                <a class="layui-btn layui-btn-xs" lay-event="es">任务状态</a>
                <a class="layui-btn layui-btn-xs" lay-event="pt">进入打印</a>
              </script>
              <script type="text/html" id="barDemo">
                  <a class="layui-btn layui-btn-xs" lay-event="able">启用</a>
                  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="disable">关闭</a>
              </script>
          </div>
        </div>
      </div>
    </div>
  </div>

  <script>
    layui.use(['admin', 'table', 'layer'], function () {
        var $ = layui.$
            , table = layui.table
            , admin = layui.admin
            , layer = layui.layer
            , form  = layui.form;


        table.render({
            elem: '#table01'
            , url: '/api/gettaskpackage/'
            , toolbar: '#toolbarDemo'
            , defaultToolbar: []
            , limit: 9999
            , initSort: {field:'create_time', type:'desc'}
            , cols: [[
                {type:'checkbox'}
                , {field: 'name', title: '名称', width: '25.1%', sort: true}
                , {field: 'count', width: '10%', title: '任务数量', sort: true}
                , {field: 'status', width: '10%', title: '状态', sort: true}
                , {field: 'editor', width: '10%', title: '编辑者'}
                , {field: 'edit_time', width: '14.8%', title: '编辑时间'}
                , {field: 'create_time', width: '14.8%', title: '创建时间', hide: true}
                , {fixed: 'right', width:'12%', title: '状态更改', align:'center', toolbar: '#barDemo'}
                , {align: 'center', width: '15%', title: '操作', fixed: 'right', toolbar: '#test-table-operate-barDemo'}

            ]]
            , done: function () {
            }
        });

        //监听工具条
        table.on('tool(table01)', function (obj) {
            var data = obj.data;
            console.log(data)
            console.log(obj)
            if (data.status === '关闭') {
                    layer.alert('当前任务包已关闭，无法进行操作', {icon: 2, shadeClose: true});
            } else {
                if (obj.event == 'es'){
                    location.hash = '/CPA/task/taskpackagetaskstatus/id=' + data.id;
                } else if (obj.event === 'ed') {
                    if (data.status !== '启用') {
                        location.hash = '/CPA/task/taskedits/id=' + data.id;
                    } else {
                        layer.alert('当前任务包已启用，无法进行操作', {icon: 2, shadeClose: true});
                    }
                } else if (obj.event === 'pt'){
                    location.hash = '/CPA/task/taskprint/id=' + data.id;
                } else if (obj.event === 'able') {
                    if (data.status !== '启用') {
                        console.log('able');
                        console.log(obj)
                        startLoading = layer.load(1);
                        admin.req({
                            method: 'POST',
                            url: '/api/changenewstatus/',
                            data: {'value': JSON.stringify(obj.data.id), "status": "启用"},
                            success: function (data) {
                                layer.close(startLoading);
                                console.log('这个是data');
                                console.log(data);
                                if (data['data'] === '1') {
                                    layer.alert(data.msg, {icon: 2}, function (index) {
                                        console.log(data);
                                        layer.close(index);
                                        layui.table.reload('table01');
                                    })
                                } else if (data['data'] === '0') {
                                    layer.alert(data.msg, {icon: 1}, function (index) {
                                        console.log(data);
                                        layer.close(index);
                                        layui.table.reload('table01');
                                    })
                                } else {
                                    layer.close(index);
                                }
                            },
                        })
                    } else {
                        layer.alert('当前任务包已启用', {icon: 2, shadeClose: true});
                    }
                }  else if (obj.event === 'disable') {
                    console.log('disable')
                    if (data.status !== '未启用') {
                        startLoading = layer.load(1);
                        admin.req({
                            method: 'POST',
                            url: '/api/changenewstatus/',
                            data: {'value': JSON.stringify(obj.data.id), "status": "关闭"},
                            success: function (data) {
                                layer.close(startLoading);
                                if (data['data'] === '1') {
                                    layer.alert(data.msg, {icon: 2}, function (index) {
                                        console.log(data);
                                        layer.close(index);
                                        layui.table.reload('table01');
                                    })
                                } else if (data['data'] === '0') {
                                    layer.alert(data.msg, {icon: 1}, function (index) {
                                        console.log(data);
                                        layer.close(index);
                                        layui.table.reload('table01');
                                    })
                                } else {
                                    layer.close(index);
                                }
                            }
                        })
                    } else {
                        layer.confirm('（谨慎）若状态更改为关闭，该任务包中所有任务都将关闭，且无法开启，请问是否要进行该操作?', {icon: 3, title:'提示'}, function(index){
                            startLoading = layer.load(1);
                            admin.req({
                                method: 'POST',
                                url: '/api/changenewstatus/',
                                data: {'value': JSON.stringify(obj.data.id), "status": "关闭"},
                                success: function (data) {
                                    layer.close(startLoading);
                                    if (data['data'] === '1') {
                                        layer.alert(data.msg, {icon: 2}, function (index) {
                                            console.log(data);
                                            layer.close(index);
                                            layui.table.reload('table01');
                                        })
                                    } else if (data['data'] === '0') {
                                        layer.alert(data.msg, {icon: 1}, function (index) {
                                            console.log(data);
                                            layer.close(index);
                                            layui.table.reload('table01');
                                        })
                                    } else {
                                        layer.close(index);
                                    }
                                }
                            });
                          layer.close(index);
                        });
                    }
                }
            }
        });

        //头部工具栏
        table.on('toolbar(table01)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            if (obj.event === 'addNewPackage') {
                layer.prompt({title: '任务包名称'}, function(value, index){
                    console.log(value);
                    admin.req({
                        method:'POST',
                        url:'/api/postnewpackagename/',
                        data:{'value':value},
                        success:function (data) {
                            layer.close(index);
                            layer.alert(data.msg, {icon: 1}, function (index) {
                                console.log(data);
                                layer.close(index);
                                layui.table.reload('table01');
                            })
                        }
                    })
                });
            } else if (obj.event === 'estimate') {
                // location.hash = '/CPA/task/taskestimate/id=' + obj.data.id;
                console.log('aaa')
                console.log(checkStatus)
                var c = '';
                var h = '';
                for (var i=0; i<checkStatus.data.length; i++){
                    c = c + checkStatus.data[i].id + ','
                }
                h=c.substr(0, c.length - 1);
                console.log(h)
                location.hash = '/CPA/task/taskestimate/id=' + h;
            }
        });
    });
  </script>